package com.example.service.webhook.business.jingdong.repository;

import com.example.service.webhook.business.jingdong.entity.JdBtBillEntity;
import com.example.service.webhook.business.jingdong.entity.JdJinTiaoBillPlanEntity;
import com.example.service.webhook.repository.base.BaseDAO;
import com.example.service.webhook.util.DateUtil;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author liyang
 * @email liyang@51dojo.com
 * @create 2017-10-24 上午11:15
 * @description 京东金条还款计划
 **/
@Repository
public class JdJinTiaoBillPlanRepository extends BaseDAO<JdJinTiaoBillPlanEntity>{

    @Resource(name="templateJingdong")
    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void saveJinTiaoBillPlan(JdJinTiaoBillPlanEntity entity)throws Exception  {
        entity.setCreateTime(DateUtil.getCurrentDate());
        entity.setLastModifyTime(DateUtil.getCurrentDate());
        this.add(entity);
    }

    public void deleteJinTiaoBillPlan(String userId,String mappingId,String loanId){
        String tableName = "t_jd_jintiaobillplan";
        StringBuilder sql = new StringBuilder("DELETE FROM ").append(tableName).append(" WHERE ").
                append("userId='").append(userId).append("'").
                append("and mappingId='").append(mappingId).append("'").
                append("and loanId='").append(loanId).append("'");

        this.getJdbcTemplate().update(sql.toString());
    }

    public JdJinTiaoBillPlanEntity getJinTiaoBillPlan(String userId,String mappingId,String planId){
        String tableName = "t_jd_jintiaobillplan";
        String sql = "select * from " + tableName + " where userId=? and mappingId=? and planId=?";
        JdJinTiaoBillPlanEntity pojo = null;
        try{
            pojo = (JdJinTiaoBillPlanEntity) queryOne(sql, new Object[] {userId,mappingId,planId}, new JdJinTiaoBillPlanEntity());
        }catch(Exception e){
            return null;
        }

        return pojo;
    }

    public void update(JdJinTiaoBillPlanEntity entity) throws Exception {
        entity.setLastModifyTime(DateUtil.getCurrentDate());
        this.modify(entity);
    }

}
